package com.fzdy.dataplatform.dao.impl;

import com.fzdy.dataplatform.core.common.TimeStampTool;
import com.fzdy.dataplatform.core.dao.impl.BaseDaoImpl;
import com.fzdy.dataplatform.dao.SyslogDao;
import com.fzdy.dataplatform.entity.Syslog;
import org.hibernate.Criteria;
import org.hibernate.criterion.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public class SyslogDaoImpl extends BaseDaoImpl<Syslog> implements SyslogDao {

    @Override
    public List<Syslog> findByExampleLike(String date,Syslog example, int pageNo, int pageSize ) {
        Criteria criteria = this.getSession().createCriteria(this.clazz).add(Example.create(example).enableLike(MatchMode.ANYWHERE));
        if (date!=null) {
            Date startTime= TimeStampTool.StringToDate("yyyy-MM-dd HH:mm:ss",date+" 00:00:00");
            Date endTime= TimeStampTool.StringToDate("yyyy-MM-dd HH:mm:ss",date+" 23:59:59");
            criteria.add(Restrictions.between("createdate", startTime,endTime));
        }if (example.getUser() != null){
            criteria.createAlias("user","user");//添加外键关联。
            criteria.add(Restrictions.like("user.name",example.getUser().getName(), MatchMode.ANYWHERE));
        }
        return this.createPageOrder(criteria, pageNo, pageSize, Order.desc("createdate")).list();
    }

    @Override
    public long countByExampleLike(String date,Syslog example) {
        Criteria criteria = this.getSession().createCriteria(this.clazz).add(Example.create(example).enableLike(MatchMode.ANYWHERE));
        if (date!=null) {
            Date startTime= TimeStampTool.StringToDate("yyyy-MM-dd HH:mm:ss",date+" 00:00:00");
            Date endTime= TimeStampTool.StringToDate("yyyy-MM-dd HH:mm:ss",date+" 23:59:59");
            criteria.add(Restrictions.between("createdate", startTime,endTime));
        }
        if (example.getUser() != null){
            criteria.createAlias("user","user");//添加外键关联。
            criteria.add(Restrictions.like("user.name",example.getUser().getName()));
        }
        Long o = (Long) criteria.setProjection(Projections.count("createdate")).uniqueResult();
        return o.longValue();
    }


}
